import { applyMiddleware, combineReducers, compose, createStore, Store } from 'redux';
import { persistReducer, persistStore } from 'redux-persist';
import storage from 'redux-persist/lib/storage';
import { thunk } from 'redux-thunk';
import global from './modules/global/reducer';
import auth from './modules/auth/reducer';
import breadcrumb from './modules/breadcrumb/reducer';

// redux持久化配置
const persistConfig = {
  key: 'redux-state',
  storage: storage,
};

// 创建reducer
const reducer = combineReducers({
  global,
  auth,
  breadcrumb,
});

// 创建一个新的redux状态的reucer
const persistReducerConfig = persistReducer(persistConfig, reducer);

// 开启redux-devtools
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

// 使用redux中间件
const middleWares = applyMiddleware(thunk);

// 创建store
const store: Store = createStore(persistReducerConfig, composeEnhancers(middleWares));

// 创建一个持久化的store
const persitor = persistStore(store);

export { store, persitor };
